package com.cy.web.action.fore;

import java.util.LinkedHashMap;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cy.bean.QueryResult;
import com.cy.bean.news.News;
import com.cy.bean.scenic.Scenic;
import com.cy.service.bus.BusLineService;
import com.cy.service.news.NewsService;
import com.cy.service.scenic.ScenicService;
import com.cy.service.search.SearchService;
import com.cy.utils.IsMoblie;
import com.opensymphony.xwork2.ActionContext;

/**
 * 首页控制器
 * @author CY
 *
 */
@Controller
@Scope("prototype")
public class IndexAction {

	@Resource
	private BusLineService busLineService;
	@Resource
	private SearchService searchService;
	@Resource 
	private ScenicService scenicService;
	@Resource
	private NewsService newsService;
	
	public String execute() {
		if(IsMoblie.isMoblie(ServletActionContext.getRequest())) {
			return "app";
		} else {
			ActionContext.getContext().put("hots", searchService.searchHotStations());
			ActionContext.getContext().put("busline", busLineService.getScrollData().getResultList());
			return "index2";
		}
	}

	@SuppressWarnings("unused")
	private String index1() {
		QueryResult<Scenic> scenics = scenicService.getScrollData();
		ActionContext.getContext().put("scenics", scenics);
		// 用于存放排序的条件
		LinkedHashMap<String, String> orderby = new LinkedHashMap<>();
		// 按照id降序排列
		orderby.put("id", "desc");
		// 查询条件where，查询允许显示的新闻
		String where = "o.news_visible=?1";
		Object[] params = {true};
		QueryResult<News> news = newsService.getScrollData(0,10,where,params,orderby);
		ActionContext.getContext().put("news", news.getResultList());
		ActionContext.getContext().put("hots", searchService.searchHotStations());
		ActionContext.getContext().put("busline", busLineService.getScrollData().getResultList());
		return "index";
	}
}
